package com.benyanyi.okhttp.type;

import android.content.Context;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;

/**
 * @author YanYi
 * @date 2019-08-12 16:49
 * @email ben@yanyi.red
 * @overview
 */
class Client {

    static OkHttpClient getClient(Context context, boolean isCache, boolean isFile) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (isFile) {
            builder.addNetworkInterceptor(HttpConfig.HTTP_LOGGING_INTERCEPTOR_FILE);
        } else {
            builder.addNetworkInterceptor(HttpConfig.HTTP_LOGGING_INTERCEPTOR);
        }
        if (isCache) {
            builder.addInterceptor(new CacheInterceptor(context))
//                .addNetworkInterceptor(CacheInterceptor())
//                .addInterceptor(HttpConfig.httpInterception())
                    .cache(HttpConfig.privateCache(context));
        }
        return builder.connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                //支持HTTPS请求，跳过证书验证
                .sslSocketFactory(SslConfig.createSSLSocketFactory(), SslConfig.getTrustManager())
                .hostnameVerifier(HttpConfig.verifier())
                .dns(new ApiDns())
                .build();
    }

}
